package com.atguigu.day01;


import com.sunfeng.TimeUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;
import java.util.function.Supplier;

/**
 * @Author : 孙峰
 * @Description:
 * @Date : 2022/6/21  9:51
 */
@Slf4j(topic = "c.Test1")
public class CompletableFutureDemo {

    private static ExecutorService executorService = Executors.newFixedThreadPool(3);


    public static void main(String[] args) throws ExecutionException, InterruptedException {

        CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
            TimeUtils.sleep(1);
            return "1";
        });



    }

    private static void m2() throws ExecutionException, InterruptedException {


        long startTime = System.currentTimeMillis();
        FutureTask<String> stringFutureTask1 = new FutureTask<>(() -> {
            TimeUtils.sleep(2);
            return "1";
        });
        executorService.submit(stringFutureTask1);
        FutureTask<String> stringFutureTask2 = new FutureTask<>(() -> {
            TimeUtils.sleep(1);
            return "2";
        });
        executorService.submit(stringFutureTask2);
        FutureTask<String> stringFutureTask3 = new FutureTask<>(() -> {
            TimeUtils.sleep(1);
            return "3";
        });
        executorService.submit(stringFutureTask3);
        executorService.shutdown();

        log.info(stringFutureTask1.get());
        long endTime = System.currentTimeMillis();
        log.info("花费的时间为：" + (endTime - startTime));

    }

    private static void m() throws ExecutionException, InterruptedException {

        FutureTask<String> stringFutureTask = new FutureTask<>(new MyThread2());
        Thread thread = new Thread(stringFutureTask, "t1");

        thread.start();
        log.info(stringFutureTask.get());
    }

    private static void m1() {
        long startTime = System.currentTimeMillis();
        TimeUtils.sleep(1);
        TimeUtils.sleep(1);
        TimeUtils.sleep(1);
        long endTime = System.currentTimeMillis();
        System.out.println("花费的时间为：" + (endTime - startTime));
    }
}

class MyThread implements Runnable {

    @Override
    public void run() {

    }
}

@Slf4j(topic = "c.MyThread2")
class MyThread2 implements Callable<String> {

    @Override
    public String call() throws Exception {
        log.info("MyThread2 call!!");
        TimeUtils.sleep(1);
        return "MyThread2";
    }
}
